package cn.palerock.dao.impl;

import cn.palerock.dao.UserDao;
import cn.palerock.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Eoly on 2017/11/1.
 */
@Repository
public class UserDaoImpl implements UserDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public UserDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<User> selectAll() {
        String sql = "select * from pr_user limit ?,?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), 0, 10);
    }

    public User selectUserById(Integer id) {
        String sql = "select * from pr_user where user_id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
    }

    public Integer insertUser(User user) {
        String sql = "insert into pr_user(user_login,user_pass,user_display) values(?,?,?)";
        return jdbcTemplate.update(sql, user.getUserLogin(), user.getUserPass(), user.getUserDisplay());
    }

    public Integer updateUserById(User user) {
        return null;
    }

    public Integer deleteUserById(Integer id) {
        return null;
    }
}
